package com.example.demo.controller;

import com.example.demo.entity.Customer;
import com.example.demo.entity.JsonResult;
import com.example.demo.mapper.CustomerMapper;
import com.example.demo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("toLogin")
public class LoginController {
    @Autowired
    CustomerService customerService;

    @Autowired
    CustomerMapper customerMapper;

    /**toLogin/addCustomer?phone=18712882023&password=1234&inviter=56&verificationCode=123
     * 用户注册
     * @param customer
     * @return
     */
    @PostMapping("addCustomer")
    public JsonResult addCustomer(Customer customer){
        JsonResult json = customerService.addCustomer(customer);
        return json;
    }

    /**toLogin/login?phone=18712882023&password=1234
     * 用户登录
     * @param customer
     * @return
     */
    @PostMapping("login")
    public JsonResult login(Customer customer){
        JsonResult json = new JsonResult();
        Integer frozen = customerMapper.getFrozen(customer.getPhone());
        if(frozen==1){
            json.setMessage("您由于违规操作，账户已被冻结");
            json.setSuccess(false);
            return json;
        }
//        if(customer.getPhone().equals("18712882023")){
        json = customerService.login(customer);
//        }else {
//            json.setMessage("系统正在升级，请明天再登陆");
//            json.setSuccess(false);
//        }
        return json;
    }
    /**toLogin/upPassword?phone=18712882023&password=1234&newPassword=12
     * 修改密码
     * @param customer
     * @return
     */
    @PostMapping("upPassword")
    public JsonResult upPassword(Customer customer){
        JsonResult json = customerService.upPassword(customer);
        return json;
    }
    /**toLogin/verificationCode?phone=18712882023
     * 注册,获取验证码
     * @return
     */
    @GetMapping(value = "/verificationCode")
    public JsonResult saveVerificationCode(String phone) {
        JsonResult json = customerService.saveVerificationCode(phone);
        return json;
    }

}
